<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS 事件机制</title>
    <style>
        #parent {
            width: 200px;
            height: 200px;
            background-color: red;
        }
        #child {
            width: 100px;
            height: 100px;
            background-color:blue;
        }
    </style>
</head>
<body onclick = "alert('2')">
    <div id="parent">
        <div id="child"></div>
    </div>
    <script>
        // document.getElementById('parent').addEventListener('click', function() {
        //     console.log('parent clicked');
        // },true);
        // document.getElementById('child').addEventListener('click', function(event) {
        //     console.log('child clicked');
        // },true);// 在捕获阶段执行
        document.getElementById('parent').addEventListener('click', function() {
            console.log('parent clicked');
        },false);
        document.getElementById('child').addEventListener('click', function(event) {
            event.stopPropagation();//阻止事件冒泡
            console.log('child clicked');
        },false);// 在冒泡阶段执行
    </script>
</body>
</html>